<template>
	<view>
		<view class="good-list">
			<view class="good-item" v-for="(item ,i) in goodsList" :key="i" @click="getGoodsDetail(item)">
				<my-goods :item="item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryOps:{
				  // 查询关键词
				  query: '',
				  // 商品分类Id
				  cid: '',
				  // 页码值
				  pagenum: 1,
				  // 每页显示多少条数据
				  pagesize: 10
				},
				//页面数据
				goodsList: [],
				//分页
				total: 0,
				// 是否正在请求数据
				isloading:false
			};
		},
		onLoad(res) {
			this.queryOps.query = res.query || ''
			this.queryOps.cid = res.cid || ''
			this.getGoodsList()
		},
		methods: {
			async getGoodsList(){
				this.isloading = true
				const {data:res} = await uni.$http.get("/api/public/v1/goods/search",this.queryOps)
				if (res.meta.status != 200) return uni.$showMessage()
				console.log(res)
				//concat : 拼接数组
				this.goodsList = this.goodsList.concat(res.message.goods)
				this.total = res.message.total
				this.isloading = false
			},
			getGoodsDetail(data){
				uni.navigateTo({
					url:"/subpkg/goods_detail/goods_detail?goods_id="+data.goods_id
				})
			}
		},
		//上拉触底事件
		onReachBottom() {
			//避免重复查询
			if(this.isloading) return
			//页码加一
			this.queryOps.pagenum += 1
			//重新获取数据
			if((this.queryOps.pagenum * this.queryOps.pagesize) >= this.total) return uni.$showSuccess("数据加载完毕")
			this.getGoodsList()
		},
		//下拉刷新事件
		onPullDownRefresh() {
			this.queryOps.pagenum = 1
			this.total = 0
			this.isloading = false
			this.goodsList = []
			//重新获取数据
			this.getGoodsList()
			uni.stopPullDownRefresh()
			
		}
	}
</script>

<style lang="scss">

</style>
